// run: $exec < input
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>

int const maxn = 200007;
int father[maxn];
int a[maxn];
int n;
struct data { int id, x; };
bool operator<(data const& a, data const& b)
{
	return a.x < b.x;
}

std::vector<data> redo;

int get_father(int x)
{
	return x == father[x] ? x : (father[x] = get_father(father[x]));
}

void set_union(int x, int y)
{
	int tx = get_father(father[x]);
	int ty = get_father(father[y]);
	if (tx != ty) father[tx] = ty;
}

int main()
{
	scanf("%d", &n);
	int count = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		father[i] = i;
	}
	std::vector<int> tr;
	for (int i = 1; i <= n; i++) {
		if (get_father(a[i]) == get_father(i)) {
			count++;
			if (a[i] == i) tr.push_back(i);
			else redo.push_back({i, i});
		} else {
			set_union(i, a[i]);
		}
	}
	for (int i = 1; i < (int)tr.size(); i++)
		redo.push_back({tr[i], tr[i]});
	std::vector<int> co;
	std::set<int> all;
	for (int i = 1; i <= n; i++) {
		co.push_back(get_father(father[i]));
		all.insert(get_father(i));
	}
	std::sort(co.begin(), co.end());
	co.erase(std::unique(co.begin(), co.end()), co.end());
	for (auto& i : redo) i.x = get_father(i.x);
	std::sort(redo.begin(), redo.end());

	for (int ti = 0; ti < (int)redo.size(); ti++) {
		auto i = redo[ti];
		if ((int)all.size() < 2) {
			if (!tr.size()) { a[i.id] = i.id; count++; }
			break;
		}
		all.erase(get_father(i.x));
		int t = *all.begin();
		all.erase(all.begin());
		a[i.id] = get_father(t);
		set_union(get_father(t), get_father(i.x));
		all.insert(get_father(t));
	}
	printf("%d\n", count - 1);
	for (int i = 1; i <= n; i++)
		printf("%d ", a[i]);
}

